<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<body>
<div class="page-container">
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">
                <ul class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">合成列表</a>
                    </li>
                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="false">添加合成</a>
                    </li>
                </ul>
                <div class="tab-content">
                    <div class=" tab-pane active" id="tab-1">
                        <form action="" class="form-inline" data-url="">
                            <div class="form-group">
                                <label class="control-label" for="key">选项:</label>
                                <select id="key" class="form-control" name="key">
                                    <option value="">请选择</option>
                                    <option value="id">ID</option>
                                    <option value="type">奖品类型</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label class="control-label" for="value">输入:</label>
                                <input class="form-control" id="value" type="text" name="value">
                            </div>
                            <button type="button" class="btn btn-primary" id="eventQuery">查询</button>
                            <button type="button" onclick="location.replace(location.href);" class="btn btn-success ">刷新</button>
                        </form>
                        <table id="tb_departments" ></table>
                    </div>
                    <div class=" tab-pane" id="tab-2">
                        <div class="ibox-content">
                            <form class="form-horizontal" enctype="multipart/form-data" id="form" data-url="/CardRuleAdd">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="type_id">
                                        <span class="c-red">*</span>所需卡片：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="card_id" id="">
                                            <option value="">--请选择--</option>
                                            {volist name='cardData' id='card'}
                                            <option value="{$card['id']}">{$card['name']}</option>
                                            {/volist}
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="card_num">
                                        <span class="c-red">*</span>所需卡片数量:
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="number" class="form-control" id="card_num"  name="card_num">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="typed">
                                        <span class="c-red">*</span>合成奖品类别：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="typed" id="typed">
                                            <option value="">--请选择--</option>
                                            <option value="1">座驾</option>
                                            <option value="2">礼物</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="type_id">
                                        <span class="c-red">*</span>合成奖品名称：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="type_id" id="type_id">
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="status">
                                        <span class="c-red">*</span>状态：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <div class="radio i-checks">
                                            <label><input type="radio"  name="status" value="0"> <i></i> 禁止用户合成</label>
                                            <label><input type="radio" name="status"  value="1"> <i></i> 开放用户合成</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="button" onclick="commonSubmit(this)">添加
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="common:footer"}
<script>
    $(document).ready(function () {
        initTable();
    });

    function initTable() {
        $('#tb_departments').bootstrapTable({
            url: '/CardRuleIndexData',         //请求后台的URL（*）
            dataField: "data",
            method: 'get',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: queryParams,//传递参数（*）
            showToggle: true,
            showRefresh: true,
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            showPaginationSwitch: true,
            minimumCountColumns: 2,             //最少允许的列数

            columns: [{
                field: 'id',
                title: 'ID',
                sortable: true
            },{
                field: 'name',
                title: '所需卡片'
            }, {
                field: 'gift_name',
                title: '合成奖品名称'
            }, {
                field: 'type',
                title: '合成奖品类型'
            },  {
                field: 'card_num',
                title: '所需卡片数量',
            },{
                field: 'Button',
                title: '操作',
                events: operateEvents,
                formatter: operateFormatter
            }]
        });
    }

    //得到查询的参数
    function queryParams(params) {
        params.offset = params.offset / params.limit + 1;
        params.key = $('#key').val();
        params.value = $('#value').val();
        console.log(JSON.stringify(params));
        return params;
    }

    function operateFormatter(value, row, index) {
        var status = row.status === 1 ? '禁止合成奖品' : '开放合成奖品';
        return [
           '<button type="button"  class="TableEdit btn-danger btn-sm" style="margin-right:15px;">修改合成奖品</button>',
            '<button type="button"  class="TableChange btn btn-danger btn-sm" style="margin-right:15px;">' + status + '</button>'

        ].join('');
    }

    window.operateEvents = {
        'click .TableChange': function (e, value, row, index) {
            changeStatus('CardRuleChangeStatus', row, this);
        },
        'click .TableEdit': function (e, value, row, index) {
            layer.open({
                type: 2,
                title: ['修改卡片合成奖品','font-size:18px;font-family:宋体;font-weight:700;'],
                shadeClose: true,
                shade: false,
                maxmin: true, //开启最大化最小化按钮
                area: ['800px', '600px'],
                content:"/CardRuleEdit?id=" + row.id,
                offset:'auto',
                scrollbar:false,
                end: function () {
                    location.reload();
                }
            });
        }
    };

    function changeStatus(url, row, obj) {
        var status = row.status === 1 ? '开放用户合成' : '禁止用户合成';
        row.status === 1 ? row.status = 0 : row.status = 1;
        $.ajax({
            type: 'get',
            dataType: 'json',
            url: url,
            data: {'id': row.id},
            success: function (re) {
                if (re.code === 200) {
                    layer.msg(re.msg, {icon: 1, time: 2000});
                    $(obj).html(status);
                } else {
                    layer.alert(re.msg);
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        });
    }

    //二级联动
    $(document).ready(function(e) {

       // ChangeType();
        $("#typed").change(function(){
           // console.log('shshfiuayf');
            ChangeType();
        });
//        $("#typed_id").change(function(){
//            ChangeGift();
//        })
    });
    function ChangeType(){
        var type = $('#typed').val();
        console.log(type);
        $.ajax({
            async:false,
            url:"/CardRuleGiftName",
            data:{type:type},
            type:"post",
            dataType:"TEXT",
            success:function(res){
                //  var hang = data.split("|");
                var data = JSON.parse(res);

                console.log(data);
                var str = "<option value='' selected>--请选择--</option>";
                if(data != null){
                    for(var i in data){
                        str+= "<option value="+data[i]['id']+">"+data[i]['gift_name']+"</option>";
                    }
                    // for(var i=0;i<data.length;i++){
                    //     str += "<option value="+data[i].id+">"+data[i].gift_name+"</option>";
                    // }
                }
                $("#type_id").html(str);
            },
            error:function(res){
                console.log(res);
            }
        });
    }
    function ChangeGift(){
        var giftId = $("#type_id").val();
//        var postData = $('#form').serialize();
//        console.log(giftId);
//        console.log(postData);
    }
    // 搜索按钮触发事件
    $(function () {
        $("#eventQuery").click(function () {
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
        });

    });
</script>
</body>
</html>